<template>
	<view>

		<u-popup v-model="show" mode="bottom">
			<view class="popup-box">

				<view class="wx">
					<view class="wx-input">
						<view class="" style="margin-bottom: 20rpx;">
							头像
						</view>
						<button type="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseavatar"
							style="width:50rpx;height: 50rpx;padding: 0;margin-bottom: 30rpx;" class="btn">
							<image v-if="headimgurl==''" src="@/static/images/my.png" style="width: 100%;height: 100%;" />
							<image v-else :src="headimgurl" style="width: 100%;height: 100%;" />
						</button>
					</view>
					<view class="wx-input">
						<view class="" style="margin-bottom: 20rpx;">
							昵称
						</view>
						<input :value="nickname" type="nickname" class="usernickname" @change="getname"
							placeholder="请输入昵称" />
					</view>

				</view>
				<view class="done" @click="login2" style="margin: 20rpx 0;font-size: 30rpx;">
					确认
				</view>
			</view>
		</u-popup>

		<view v-if="!isLogin" class="appwx" @click="wxlogin()">
			<u-image width="130rpx" height="130rpx" src="@/static/images/weixin.png"></u-image>
			<view style="color: #F44C01;font-size: 40rpx;margin-top: 40rpx;">微信一键登录</view>
		</view>
		<view v-else>
			<view class="uni-form-item uni-column">
				<view class="cell">
					<text class="label">发布标题：</text>
					<u-input v-model="form.title" placeholder="请输入标题" />
				</view>
				<view class="cell">
					<text class="label">工作条件：</text>
					<u-input v-model="form.limit" placeholder="多条件请用“|”隔开" />
				</view>
				<view class="cell">
					<text class="label">工作福利：</text>
					<u-input v-model="form.welfare" placeholder="多条件请用“|”隔开" />
				</view>
				<view class="cell">
					<text class="label">联系人：</text>
					<u-input v-model="form.contact_name" placeholder="请输入联系人" />
				</view>
				<view class="cell">
					<text class="label">联系电话：</text>
					<u-input v-model="form.contact" placeholder="请输入联系电话" type="number" />
				</view>
				<view class="cell ">
					<text class="label">工作地址：</text>
					<u-input v-model="form.address" placeholder="请输入工作地址" />
				</view>
				<view class="cell cell1">
					<text class="label">工作内容：</text>
					<textarea @blur="bindTextAreaBlur" class="u-flex-1 u-textarea-class" v-model="form.content"
						placeholder="请输入工作内容" />
				</view>
				<view class="botbtn" style="width: 80%;margin: 60rpx;margin-left: 10%;">
					<u-button style="flex: 1;" type="primary" @click="confirem">发布信息</u-button>
				</view>

			</view>

		</view>
	</view>
</template>




<script>
	import {
		APP_NAME
	} from '../../env.js'
	import {
		mapActions,
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				headimgurl: '',
				userImg: "http://tmp/RcJvKcGByqqv66ff334952c4d6f86ebfa29b3148942f.jpeg",
				nickname: '',
				form: {
					title: '',
					limit: '',
					contact_name: '',
					contact: '',
					address: '',

					content: ''
				},
				show: false,
				modelshow: false,
				showType: 'job',
				opentop: false,
			}
		},
		onLoad() {
			if (this.isLogin) {
				// this.$u.route(path)
				// return this.$u.toast('完整演示请咨询');
			} else {
				// this.$u.toast('请先登录');
				// setTimeout(()=>{this.$u.route('/pages/user/user')},1000)

			}
		},
		onShareAppMessage() {
			return {
				title: APP_NAME,
				path: '/pages/index/index'
			}
		},
		computed: {
			...mapState({
				userInfo: state => state.user.userInfo,
				isLogin: state => state.user.isLogin,
			})
		},
		methods: {
		
			bindTextAreaBlur: function(e) {
				this.form.content = e.detail.value
				console.log(e.detail.value)
			},
			...mapActions([
				'login',
				'loginOut'
			]),
			getname(e) {
				console.log(e)
				this.nickname = e.detail.value

			},
			changeUserName1(e) {},
			onChooseavatar(e) {
				let that = this
				that.userImg = e.detail.avatarUrl
				var uploadurl = "https://ship.jiankaobang.com/api/upload/image"
				uni.uploadFile({
					url: uploadurl,
					filePath: that.userImg,
					name: 'file',
					header: {
						token: uni.getStorageSync('token')
					},
					fileType: 'image',

					// formData: {
					//     'openid': '123456'
					// },

					success: (uploadFileRes) => {
						let data = JSON.parse(uploadFileRes.data)
						if (data.code == 1) {
							that.headimgurl = data.data.uri;
						}
					}
				});
			},
			confirem() {

				if (this.userInfo.is_release == '2') {
					this.$u.toast('账号无发布权限！！')
					return
				}
				this.$u.post('/api/recruit/release', this.form).then(res => {
					this.$u.toast({
						type: 'loading'
					})
					this.$u.toast('已提交！等待审核中')
					this.form = {}
					uni.navigateBack({
						delta: 1
					});

				}).catch(err => {})
			},
			login2() {
				var that = this;
				if (that.nickname == '') {
					this.$u.toast('请输入昵称')
					return
				}
				if (that.headimgurl == '') {
					this.$u.toast('请上传头像')
					return
				}
				uni.showLoading({
					title: '正在加载中...'
				})
				that.$u.post('/api/login/updateUser', {
					nickname: that.nickname,
					avatar: that.headimgurl
			
				}).then(res => {
					that.show = false
					console.log(22222, that.sessionRes, )
					that.sessionRes.avatar = that.headimgurl
					that.sessionRes.nickname = that.nickname,
						console.log(33333, that.sessionRes, )
					// that.sessionRes
					that.login(that.sessionRes);
			
			
				})
			},
			
			wxlogin() {
				var that = this;
				uni.showLoading({
					title: '正在加载中...'
				})
				uni.getUserProfile({
					desc: '需要获取您的微信头像以及昵称',
					lang: 'zh_CN',
					success: function(infoRes) {
						console.log(infoRes.rawData);
						uni.login({
							provider: 'weixin',
							success: function(loginRes) {
								console.log(loginRes);

								that.$u.post('/api/login/mnplogin', {
									code: loginRes.code,

								}).then(sessionRes => {
									if(sessionRes.is_new_user==0){
										that.login(sessionRes) 
									}else{
									that.show = true
									uni.setStorageSync('token', sessionRes.token)
									that.sessionRes = sessionRes	
									}
								



								})
							}
						});
					}
				});
			},
			openModel(type) {
				this.showType = type;
				this.modelshow = true;
			},
			navTo(path) {
				console.log(this.form)
				this.modelshow = false;
				this.$u.route(path)
				return

			}
		}
	}
</script>
<style>
	page {
		background-color: white;
	}
</style>
<style lang="scss" scoped>
	.gird {
		background-color: white;
		border-radius: 20rpx;
	}

	.cell {
		display: flex;
		flex-direction: row;
		align-items: center;
		margin-left: 20rpx;
		margin-right: 20rpx;
		padding: 20rpx;
		border-bottom: 1rpx solid #e6e6e6;

		.label {
			padding-left: 20rpx;
			padding-right: 20rpx;
			font-size: 30rpx;
		}
	}

	.cell1 {
		align-items: flex-start;
	}

	.appwx {
		margin-top: 28vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.modelcont {
		padding: 30rpx;
		width: 500rpx;

		.cont {
			.cell {
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;
				height: 80rpx;
				font-size: 30rpx;
				color: #333333;
				background-color: #F1F1F1;
				border-radius: 10rpx;
				margin-bottom: 20rpx;
			}
		}

		.close {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: center;
			height: 50rpx;
		}
	}

	.popup-box {
		text-align: center;

		.wx-input {
			margin: 20rpx 0;
		}
	}
</style>